
import UtilsBindingTarget from '../Utils.BindingTarget/UtilsBindingTarget';

const _timeBar = Symbol('_timeBar');
const _bloodBar = Symbol('_bloodBar');
const _error = Symbol('_error');
export default class UtilsBar {
    constructor(_type, _options, _that, _callback) {
        this.options = _options;
        this.callback = _callback;
        if (!this.options) return this[_error]('no options?');
        if (!_that) return this[_error]('no this?');
        _type === 'bloodBar' ? this[_bloodBar](_that) : this[_timeBar](_that);
    }

    [_timeBar](_that) {
        let num = 0;
        const bar = _that.add.easyProgressbar(this.options.x, this.options.y, this.options.w, this.options.h, {
            color: this.options.color,  // 0x...
            radius: this.options.radius,
            progress: num,
            text: {
                enabled: this.options.enabled ? this.options.enableds : true,
                format: progress => `${this.options.showText}: ${(progress * 100).toFixed(0)}%`,
                origin: {
                    x: this.options.text.x,
                    y: this.options.text.y
                }
            }
        });
        let timeBarA = setInterval(() => {
            num = num += this.options.timeNum;
            bar.setProgress(num, true);
            if (num >= 1) {
                clearInterval(timeBarA);
                this.callback('Bar Time Don!');
                bar.destroy();
            }
        }, this.options.timeOut);
    }

    [_bloodBar](_that) {
        _that[this.options.npcBarKey] = _that.add.easyProgressbar(this.options.x, this.options.y, this.options.w, this.options.h, {
            color: this.options.color,
            radius: this.options.radius,
            progress: this.options.bloodProgr,
            text: {
                enabled: this.options.enabled ? this.options.enableds : true,
                format: progress => `${this.options.showText}: ${(progress * 100).toFixed(0)}%`,
                origin: {
                  x: this.options.text.x,
                  y: this.options.text.y
                }
            }
        });

        new UtilsBindingTarget([{
            key: this.options.npcBarKey,
            targetKey: this.options.bindTar.tarKey,
            off: {
                x: this.options.bindTar.off.x,
                y: this.options.bindTar.off.y
            },
            rot: this.options.bindTar.rot,
            rotFF: this.options.bindTar.rotFF
        }], _that);
    }

    [_error](err) {
        throw err;
    }

}